#define __HEX_DIGITS 4

#macro hexloop(zval)
  #if gcount <= (__HEX_DIGITS-1) || (zval != 0 && zval != $FFFFFFFF)
    #define hex_digit eval(zval & $F)
    #if hex_digit = 0
      #define ghex concat("\"0\",",ghex)
    #endif
    #if hex_digit = 1
      #define ghex concat("\"1\",",ghex)
    #endif
    #if hex_digit = 2
      #define ghex concat("\"2\",",ghex)
    #endif
    #if hex_digit = 3
      #define ghex concat("\"3\",",ghex)
    #endif
    #if hex_digit = 4
      #define ghex concat("\"4\",",ghex)
    #endif
    #if hex_digit = 5
      #define ghex concat("\"5\",",ghex)
    #endif
    #if hex_digit = 6
      #define ghex concat("\"6\",",ghex)
    #endif
    #if hex_digit = 7
      #define ghex concat("\"7\",",ghex)
    #endif
    #if hex_digit = 8
      #define ghex concat("\"8\",",ghex)
    #endif
    #if hex_digit = 9
      #define ghex concat("\"9\",",ghex)
    #endif
    #if hex_digit = 10
      #define ghex concat("\"A\",",ghex)
    #endif
    #if hex_digit = 11
      #define ghex concat("\"B\",",ghex)
    #endif
    #if hex_digit = 12
      #define ghex concat("\"C\",",ghex)
    #endif
    #if hex_digit = 13
      #define ghex concat("\"D\",",ghex)
    #endif
    #if hex_digit = 14
      #define ghex concat("\"E\",",ghex)
    #endif
    #if hex_digit = 15
      #define ghex concat("\"F\",",ghex)
    #endif
    #undef hex_digit
    #define gcount eval(gcount+1)
    hexloop(zval>>4)
  #else
    #define ghex concat("\"$\",",ghex)
  #endif
#endmacro

#macro generate_ghex(zval)
  #define ghex ""
  #define gcount 0
  hexloop(zval)
#endmacro

#macro echohexadj(a)
  #define error $CCCCCCCC
  .echo > echohex.tmp "#define ga ", a, "\n"
  #include "echohex.tmp"
  .echo > echohex.tmp "#define ga ", ga, "\n"
  #include "echohex.tmp"
  #if ga=error
    #define ga eval(a)
  #else
    generate_ghex(a)
    #define ga eval(ghex)
  #endif
  #undef error
#endmacro

#macro echohex(aa1, aa2, aa3, aa4, aa5)
  #ifdef aa5
    .error "Too many arguments to echohex, only 4 implemented"
  #endif

  #ifdef aa1
    echohexadj(aa1)
    #define aa1 eval(ga)
  #endif

  #ifdef aa2
    echohexadj(aa2)
    #define aa2 eval(ga)
  #endif

  #ifdef aa3
    echohexadj(aa3)
    #define aa3 eval(ga)
  #endif

  #ifdef aa4
    echohexadj(aa4)
    #define aa4 eval(ga)
  #endif

  #ifndef aa1
    #define aa1 " "
  #endif
  #ifndef aa2
    #define aa2 " "
  #endif
  #ifndef aa3
    #define aa3 " "
  #endif
  #ifndef aa4
    #define aa4 " "
  #endif

  .echo aa1,aa2,aa3,aa4

  #undef aa1
  #undef aa2
  #undef aa3
  #undef aa4
#endmacro

#define .echohex echohex

;.echo "Decimal value: ", $1234
;.echohex "Hex value: ",$1234
